<?php

include_once ('lib/parvis.constants.php');
require_once ('lib/class.http.php');
require_once ('lib/dump.php');
require_once ('lib/class.urls.php');

// Retrouver la langue de l'URL
$langage = urls::getLangNum();

$api = new Http();
//ob_start();

//$url = 'http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/tbr_article.w';
$url = sprintf('%stbr_article.w', WEBSPEED);
$api->useCurl(false);
ob_start();

if (empty($_POST['navigue'])) {
	
	// 1er appel: GET
    $api->setMethod('GET');
	$api->addParam('lang', $langage);
	$api->addParam('langue', $langage);
	
	$search = $_GET['search'];
	
	if (empty($search) || $search==="") {
    	$api->addParam('lib', '*');
	} else {
		$api->addParam('lib', utf8_decode($search));
	}

    $mode= $_GET['mode'];
    if (!empty($mode)) {
		if ($mode === "N"){//nouveauté
			$api->addParam('stat', 'N');
		}else if ($mode === "P"){//promotion
			$api->addParam('stat', 'P');
		}else if ($mode === "L"){//en solde
			$api->addParam('stat', 'L');
		}
    }

} else {
	// Appels suivants (via un des boutons de l'interface)
    $api->setMethod('POST');
    // Les paramètres de POST sont redécodés en ISO
	if (!(empty($_POST['lib']) || $_POST['lib']==="")) {
		$_POST['statq'] = "";
	}
	
    foreach ($_POST as $key => $post) {
		if(is_array($_POST[$key])){
			$_POST[$key] = implode($_POST[$key]);
		}
	    $param = utf8_decode($_POST[$key]);
	    $api->addParam($key, $param);
	}
	$api->addParam('lang', $langage);	
}

$api->useCookie(true);
$api->saveCookie(true);
$api->addCookie('SessionId', $_COOKIE['SessionId']);
$api->addCookie('InterNou', $_COOKIE['InterNou']);



$api->execute($url);

$cookies = $api->_cookies;

if ($debug) {
	dump($cookies, 'Cookies retourné par le serveur');

	dump($cookies[0]['value'], 'value');
}

if (!empty($cookies[0])) {
	// Stockage "local" du cookie retourné par le serveur
	if ($cookies[0]['name'] == 'InterNou') {
		setcookie('InterNou', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
	if ($cookies[0]['name'] == 'SessionId') {
		setcookie('SessionId', $cookies[0]['value'], COOKIES_TIME, COOKIES_PATH);
	}
}

echo ($api->error) ? $api->error : utf8_encode($api->result);

	$content = ob_get_contents();
	ob_end_clean();
	/*$dom = new DOMDocument;
	$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
	$dom->recover = true;
	$dom->strictErrorChecking = false;
	@$dom->loadHTML($content);  

	$imgs = $dom->getElementsByTagName("img");
	foreach($imgs as $img){
		//theme('imagecache', 'parametreimagecache', $field_champimage[0]['filepath'])  + change src
		$path = $img->getAttribute('src');
		if(strpos($path,"images_e") !== false){
			//$src = imagecache_external_generate_path('catalogue',$base_url."/sites/default/files".$path);
			$src = "/sites/default/files/imagecache/catalogue/sites/default/files".$path;
			//$src = imagecache_external_generate_path('catalogue', $base_url.$path);
			$img->setAttribute( 'src' , $src );
		}
	}	
	
	$body = $dom->getElementsByTagName('body');
	if ( $body && 0<$body->length ) {
		$body = $body->item(0);
		echo $dom->savehtml($body);
	}*/
	echo"$content";
?>
